package edu.ctbu.ctbujsj.controller;


import edu.ctbu.ctbujsj.domain.Student;
import edu.ctbu.ctbujsj.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "/student/list")
     public String  List(HttpServletRequest request, ModelMap map,
                         @RequestParam(defaultValue = "0") int pageindex,
                         @RequestParam(defaultValue = "2") int pagesize) {

        HttpSession session=request.getSession();
        if(session.getAttribute("userid")==null){
            //没有登录，转登录

            return "redirect:/login/student";


        }


        Page<Student> myList=studentService.findByPage(pageindex,pagesize);



        map.addAttribute("totalPage",myList.getTotalPages());
        map.addAttribute("totalElement",myList.getTotalElements());


        map.addAttribute("students",myList.getContent());


         return "/student/list";
     }

}
